<?php
/**
 * This is users/admin_edit template file. 
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil.views.users
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

$showSelectAreas = !empty($areas) && $this->LilForm->value('User.id') && !$this->Lil->role('Lil.User', 'admin', $this->LilForm->value('User.id'));
 
$user_edit = array(
	'title_for_layout' =>
		($this->Form->value('User.id') ? __d('lil', 'Edit User') : __d('lil', 'Add User')),
	'menu' => array(
		'delete' => array(
			'title' => __d('lil', 'Delete', true),
			'visible' => $this->LilForm->value('User.id'),
			'url'   => array(
				'admin'      => true,
				'plugin'     => 'lil',
				'controller' => 'users',
				'action'     => 'delete',
				$this->LilForm->value('User.id'),
			),
			'params' => array(
				'confirm' => __d('lil', 'Are you sure you want to delete this user?')
			)
		),
	),
	'form' => array(
		'pre' => '<div class="form">',
		'post' => '</div>',
		'lines' => array(
			'form_start' => array(
				'class'      => $this->LilForm,
				'method'     => 'create',
				'parameters' => array('model' => 'User')
			),
			'id' => array(
				'class'      => $this->LilForm,
				'method'     => 'hidden',
				'parameters' => array('field' => 'id')
			),
			'referer' => array(
				'class'      => $this->LilForm,
				'method'     => 'hidden',
				'parameters' => array('field' => 'referer')
			),
			'fs_basics_start' => '<fieldset>',
			'lg_basics' => sprintf('<legend>%s</legend>', __d('lil', 'Basics')),
			'name' => array(
				'class'      => $this->LilForm,
				'method'     => 'input',
				'parameters' => array(
					'field'   => Configure::read('Lil.userDisplayField'),
					'options' => array(
						'type'  => 'text',
						'label' => __d('lil', 'Name') . ':',
						'error' => __d('lil', 'User\'s name is required.'),
						'class' => 'big'
					)
				)
			),
			'username' => array(
				'class'      => $this->LilForm,
				'method'     => 'input',
				'parameters' => array(
					'field'   => 'username',
					'options' => array(
						'type'  => 'text',
						'label' => __d('lil', 'Username') . ':',
						'error' => __d('lil', 'Username is required, format must be valid.'),
					)
				)
			),
			'fs_basics_end' => '</fieldset>',
			
			'fs_passwords_start' => '<fieldset>',
			'lg_passwords' => sprintf('<legend>%s</legend>', __d('lil', 'Change password')),
			'passwd' => array(
				'class'      => $this->LilForm,
				'method'     => 'input',
				'parameters' => array(
					'field'   => 'passwd',
					'options' => array(
						'type'  => 'password',
						'label' => __d('lil', 'New password') . ':',
						'error' => __d('lil', 'Password is required, format must be valid.'),
					)
				)
			),
			'new_pass' => array(
				'class'      => $this->LilForm,
				'method'     => 'input',
				'parameters' => array(
					'field'   => 'new_pass',
					'options' => array(
						'type'  => 'password',
						'label' => __d('lil', 'Repeat password') . ':',
						'error' => __d('lil', 'Passwords do not match.'),
					)
				)
			),

			'fs_passwords_end' => '</fieldset>',
			
			// display areas only if user is not administrator,
			'fs_areas_start' => !$showSelectAreas ? null : '<fieldset>',
			'lg_areas' => !$showSelectAreas ? null : sprintf('<legend>%s</legend>', __d('lil', 'User Areas')),
			'areas' => !$showSelectAreas ? null : array(
				'class'      => $this->LilForm,
				'method'     => 'input',
				'parameters' => array(
					'field' => 'Area.Area',
					'options' => array(
						'label' => __d('lil', 'Select areas') . ':',
					)
				)
			),
			'fs_areas_end' => !$showSelectAreas ? null : '</fieldset>',
			
			'submit' => array(
				'class'      => $this->LilForm,
				'method'     => 'submit',
				'parameters' => array(
					'label' => __d('lil', 'Save')
				)
			),
			'form_end' => array(
				'class'      => $this->LilForm,
				'method'     => 'end',
				'parameters' => array()
			),
		)
	)
);

if ($this->Form->value('User.id')) {
	$user_edit = $this->callPluginHandlers('form_edit_user', $user_edit);
} else {
	$user_edit = $this->callPluginHandlers('form_add_user', $user_edit);
}

$this->Lil->form($user_edit);